package com.itheima.admin.controller;


import com.itheima.admin.dto.ChannelPageRequestDto;
import com.itheima.admin.pojo.AdChannel;
import com.itheima.admin.service.AdChannelService;
import com.itheima.common.enums.HttpCodeEnum;
import com.itheima.common.vo.PageResultVo;
import com.itheima.common.vo.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @version 1.0
 * @description <p>频道信息</p>
 * @package com.itheima.admin.controller
 */
@RestController
@RequestMapping("/channel")
@Api(tags = "WmMaterial")
public class AdChannelController {

    @Autowired
    private AdChannelService adChannelService;

    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @PostMapping("/list")
    @ApiOperation("频道分页查询")
    public PageResultVo<AdChannel> pageList(@RequestBody ChannelPageRequestDto dto) {
        PageResultVo<AdChannel> pageResultVo = adChannelService.pageList(dto);
        return pageResultVo;
    }

    /**
     * 通过id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("id查询")
    public ResultVo findById(@PathVariable Integer id){
        return  ResultVo.ok(adChannelService.getById(id)) ;
    }

    @PostMapping("/update")
    @ApiOperation("更新频道")
    public ResultVo update(@RequestBody AdChannel adChannel){
        if(null == adChannel || null == adChannel.getId()){
            return ResultVo.error(HttpCodeEnum.PARAM_REQUIRE);
        }
        if (adChannelService.updateById(adChannel)) {
            return ResultVo.ok();
        }
        return ResultVo.error();

    }

    /**
     * 新增频道
     *
     * @param adChannel
     * @return
     */
    @PostMapping("/save")
    @ApiOperation("新增频道")
    public ResultVo save(@RequestBody @Validated AdChannel adChannel) {
        adChannel.setIsDefault(false);
        adChannel.setCreatedTime(new Date());
        return adChannelService.save(adChannel) ? ResultVo.ok() : ResultVo.error();
    }


    /**
     * 删除频道
     * @param id
     * @return
     */
    @DeleteMapping("/del/{id}")
    @ApiOperation("删除频道")
    public ResultVo deleteById(@PathVariable(value = "id") Integer id){
        if(adChannelService.removeById(id)){
            return ResultVo.ok();
        }
        return ResultVo.error();
    }


}

